package com.doubao.doubao_admin.controller;

import com.doubao.doubao_admin.common.Result;
import com.doubao.doubao_admin.model.entity.UmsUser;
import com.doubao.doubao_admin.model.dto.LoginDTO;
import com.doubao.doubao_admin.model.dto.RegisterDTO;
import com.doubao.doubao_admin.service.cService.UmsUserService;
import com.doubao.doubao_admin.utils.JwtUtils;
import com.doubao.doubao_admin.utils.MD5;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;

import static com.doubao.doubao_admin.utils.JwtUtils.USER_JNAME;

@RestController
@RequestMapping("/ums/user")
public class UmsUserController {
    @Resource
    UmsUserService umsUserService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    /**
     * 注册接口的逻辑处理
     * 判断用户名、密码、手机号是否为空，如果为空，注册失败；
     * 如果都不为空，根据用户名查询是否有重复用户名，如果有，注册失败；
     * 如果没有重复用户名，注册成功
     */
    public Result userRegister(@RequestBody RegisterDTO dto) throws NoSuchAlgorithmException {

        String username = dto.getName();
        String password = dto.getPass();
        String email = dto.getEmail();

        //1.判断用户名、密码、手机号是否为空
        if (username != null && password != null && email != null) {
            UmsUser users = umsUserService.getUserByUsername(username);
            //2.判断是否有重复用户名
            if (users != null) {
                return Result.error("-1", "注册失败，用户名重复,请更换", null);
            } else {
                //3.没有重复用户名，注册成功
                UmsUser umsUser = umsUserService.executeRegister(dto);
                if (!ObjectUtils.isEmpty(umsUser)) {//注册成功
                    return Result.success("0", "账号注册成功！");
                }
            }
        } else {
            return Result.error("-1", "注册失败，请检查用户名、密码、邮箱是否为空");
        }
        return Result.error("-1", "操作失败");
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody LoginDTO dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();

        //1. 判断用户名、密码是否为空
        if (username != null && password != null) {
            UmsUser users = umsUserService.getUserByUsername(username);
            //2. 判断用户名是否存在
            if (users != null) {
                //3. 判断密码是否正确
                String pass = MD5.getMD5(password);
                String token = umsUserService.executeLogin(dto);
                if (ObjectUtils.isEmpty(token)) {
                    return Result.error("-1", "账号密码错误");
                } else {

                    int result = JwtUtils.verifyToken(token);

                    if (result != 0) {
                        return Result.error("-1", "token验证失败");
                    }
                }
                users.setToken(token);
                if (pass.equals(users.getPassword())) {
                    //4. 密码正确，登录成功
                    return Result.success("0", "登录成功", token);//"登录成功"
                } else {
                    return Result.error("-1", "登录失败，密码错误", null);
                }
            } else {
                return Result.error("-1", "登录失败，用户名不存在", null);
            }
        } else {
            return Result.error("-1", "登录失败，请检查用户名、密码是否为空", null);
        }
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
/**
 * RequestHeader这个注解从请求对象里找请求头里，看看请求头里有没有userName这个属性，
 * 如果有，把这个属性拿到传给String userName的变量，与JwtUtils进行配合
 */
    public Result<UmsUser> getUser(@RequestHeader(value = USER_JNAME)  String username) {
        UmsUser user = umsUserService.getUserByUsername(username);
        return Result.success(user);
    }

    /**
     * 注销成功接口
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Result<Object> logout() {//收到注销请求直接返回Result
        return Result.success(null,"注销成功");
    }
}
